﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPITest.Filter;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ResultFilter]
    
    public class HomeController : Controller
    {
        //定义服务变量 生命周期
        private readonly IDataService _dataService;
        private readonly IScopedService _scopedService;
        private readonly ISingletonService _singletonService;
        private readonly ITransientService _transientService;
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <returns></returns>
        
        public HomeController(IDataService dataService, IScopedService scopedService,ITransientService transientService,ISingletonService singletonService)
        {
            _dataService = dataService;
            _scopedService = scopedService;
            _singletonService = singletonService;
            _transientService = transientService;
            
            
        }
        /// <summary>
        /// 方法注入 从方法取
        /// </summary>
        /// <param name="dataService2">服务类的接口 变量名</param>
        [HttpGet]
        public void Index([FromServices]IDataService dataService2,[FromServices] IScopedService scopedService2,[FromServices]ITransientService transientService,[FromServices]ISingletonService singletonService2)
        {
            //1.需要自己实例化类 一旦类发生修改就会影响很多地方
            //DataService dataService = new DataService();
            //dataService.GetData();

            //2依赖注入 把new实例化 这一步交给IOC容器去完成
            _dataService.GetData();
            dataService2.GetHashCode();
            //这是从构造方法取得
            var s = _scopedService.GetHashCode();
            var s1 = scopedService2.GetHashCode();
            //这个实验 瞬时的 
            var t1 = transientService.GetHashCode();
            var t2 = transientService.GetHashCode();
            //
            //var s2 = _singletonService.GetHashCode();
            //var s3 = singletonService2.GetHashCode();
        }
        //无论获取多少次 都是同一个
        [HttpGet]
        public int SingletonFun()
        {
             return _singletonService.GetHashCode();
        }
        //每次从IOC容器获取都是不同一个对象
        [HttpGet]
        public string TransientFun([FromServices] ITransientService transientService2)
        {
          var t1=_transientService.GetHashCode();
          var t2 = transientService2.GetHashCode();
            return $"t1={t1},t2={t2}";
        }
        //当我们同一个请求内 每次从IOC获取对象时 都是同一个，在不同的请求 会是补一样的对象
        [HttpGet]
        public string ScopedFun([FromServices]IScopedService scopedService2)
        {
            var s1 = _scopedService.GetHashCode();
            var s2 = scopedService2.GetHashCode();
            return $"s1={s1},s2={s2}";
        }
        //异常处理
        //public void Test()
        //{
        //    ClassA  a= null;
        //    a.GetHashCode();
        //}

        //需求：利用过滤器统一给接口的参数 判断有无异常关键字
        //(用判断字符串是否包含"哈"这个字来代替这个逻辑)
        //提示可以使用 context.HttpContext.Request.Query 来获取接口请求的所有Query参数
        /// <summary>
        /// 给一个用户发消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="memberId"></param>
        [HttpGet]
        public string SendMessage(string message, int memberId)
        {
            //发消息的逻辑
            return"请求成功";
        }

        /// <summary>
        /// 发评论接口 给视频发评论
        /// </summary>
        [HttpGet]
        public string SendComment(string message, int vod)
        {
            //发评论
            return"请求成功";
        }
        //cookie 记录在浏览器上面 携带用户信息给服务器
        //session会话 aspx跟服务器保持会话连接 缺点：耗服务器

        [HttpGet]
        //添加客户端缓存
        [ResponseCache(Duration =20)]
        public DateTime Now()
        {
            return DateTime.Now;
        }
    }
}
